﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;

using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Spreadsheets;

namespace DataSources
{
    public struct WorksheetInfo // TODO: remove and use only WorksheetEntry
    {
        public string title;
        public string cellsFeedUri;
        public string listFeedUri;
        public WorksheetEntry entry;

        public WorksheetInfo(string title, string cellsFeedUri, string listFeedUri, WorksheetEntry entry)
        {
            this.title = title;
            this.cellsFeedUri = cellsFeedUri;
            this.listFeedUri = listFeedUri;
            this.entry = entry;
        }

        public static WorksheetInfo Empty = new WorksheetInfo();
    }

    public class GoogleWorksheets : IDataSource
    {
        protected SpreadsheetsService service = null;
        protected GoogleSpreadsheets owner = null;
        protected string ownerCurrentEntryHash;
        protected SpreadsheetInfo CurrentSpreadsheet
        {
            get
            {
                return owner.GetEntryInfo(ownerCurrentEntryHash);
            }
        }
        protected Hashtable worksheets = null;

        public GoogleWorksheets(SpreadsheetsService service, GoogleSpreadsheets owner, string ownerCurrentEntryHash)
        {
            this.service = service;
            this.owner = owner;
            this.ownerCurrentEntryHash = ownerCurrentEntryHash;

            RequestData();
        }

        private void RequestData()
        {
            if (this.CurrentSpreadsheet.Equals(SpreadsheetInfo.Empty))
                return;

            // load aspects data (worksheets)
            WorksheetQuery queryWorksheet = new WorksheetQuery(this.CurrentSpreadsheet.feedUri);
            WorksheetFeed feedWorksheet = this.service.Query(queryWorksheet);
            this.worksheets = new Hashtable();
            AtomEntryCollection worksheetEntries = feedWorksheet.Entries;
            for (int i = 0; i < worksheetEntries.Count; i++)
            {
                WorksheetEntry worksheetEntry = worksheetEntries[i] as WorksheetEntry;

                // TODO: avoid copy-paste!

                // Get the cells feed URI
                AtomLink cellsLink = worksheetEntry.Links.FindService(
                    GDataSpreadsheetsNameTable.CellRel,
                    AtomLink.ATOM_TYPE
                );

                // Get the list feed URI
                AtomLink listLink = worksheetEntry.Links.FindService(
                    GDataSpreadsheetsNameTable.ListRel,
                    AtomLink.ATOM_TYPE
                );

                // Create an item to store the title and the worksheet's feed URI
                worksheets.Add(
                    worksheetEntry.Id.AbsoluteUri,
                    new WorksheetInfo(
                        worksheetEntry.Title.Text +
                        " (Rows:" + ((WorksheetEntry)worksheetEntry).RowCount.Count +
                        ", Cols:" + ((WorksheetEntry)worksheetEntry).ColCount.Count + ")",
                        cellsLink.HRef.Content,
                        listLink.HRef.Content,
                        worksheetEntry
                    )
                );
            }
        }

        public bool IsAuthOK()
        {
            return true;
        }

        public string GetOwnerHash()
        {
            return CurrentSpreadsheet.entry.Id.AbsoluteUri;
        }

        public void ReloadData()
        {
            RequestData();
        }

        public WorksheetInfo GetEntryInfo(string entryHash)
        {
            if (worksheets.ContainsKey(entryHash))
                return (WorksheetInfo)worksheets[entryHash];
            return WorksheetInfo.Empty;
        }
    }
}
